package com.bcxj.servicemap.remote;

import com.bcxj.internalcommon.constant.AmapConfigConstants;
import com.bcxj.internalcommon.response.DirectionResponse;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * ClassName: MapDirectionClient
 * Package: com.bcxj.servicemap.remote
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/10/5 17:03
 * @Version 1.0
 * @Since 1.0
 */
@Service
@Slf4j
public class MapDirectionClient {

    @Value("${amap.key}")
    private String AMAP_KEY;

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 调用远程API获取距离和时间信息
     * @param depLongitude
     * @param depLatitude
     * @param destLongitude
     * @param destLatitude
     * @return
     */
    public DirectionResponse direction(String depLongitude, String depLatitude, String destLongitude, String destLatitude){
        // 拼接url
        //https://restapi.amap.com/v3/direction/driving?origin=106.675644,26.617866&destination=106.673891,26.443701&extensions=all&output=json&key=556b1991b6ec5f24d148342a93b20e79
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(AmapConfigConstants.DIRECTION_URL);
        urlBuilder.append("?");
        urlBuilder.append("origin=").append(depLatitude).append(",").append(depLongitude);
        urlBuilder.append("&destination=").append(destLatitude).append(",").append(destLongitude);
        urlBuilder.append("&extensions=all");
        urlBuilder.append("&output=json");
        urlBuilder.append("&key=").append(AMAP_KEY);
        System.out.println(urlBuilder.toString());

        // 解析结果
        ResponseEntity<String> entity = restTemplate.getForEntity(urlBuilder.toString(), String.class);

        System.out.println(entity.getBody());

        DirectionResponse directionResponse = parseEntityJson(entity.getBody());
        return directionResponse;
    }

    private DirectionResponse parseEntityJson(String body) {
        JSONObject jsonObject = JSONObject.fromObject(body);

        if(!jsonObject.has(AmapConfigConstants.STATUS) || !jsonObject.getString(AmapConfigConstants.STATUS).equals("1")){
            return null;
        }

        if(!jsonObject.has(AmapConfigConstants.ROUTE) || !jsonObject.getJSONObject(AmapConfigConstants.ROUTE).has("paths")){
            return null;
        }

        JSONObject route = jsonObject.getJSONObject(AmapConfigConstants.ROUTE);

        JSONObject paths = route.getJSONArray(AmapConfigConstants.PATHS).getJSONObject(0);

        if(!paths.has(AmapConfigConstants.DISTANCE) || !paths.has(AmapConfigConstants.DURATION)){
            return null;
        }

        int distance = paths.getInt(AmapConfigConstants.DISTANCE);
        int duration = paths.getInt(AmapConfigConstants.DURATION);
        DirectionResponse directionResponse = new DirectionResponse(distance, duration);

        return directionResponse;
    }
}
